<!--
 * @Author: your name
 * @Date: 2022-03-30 15:30:40
 * @LastEditTime: 2022-03-30 15:32:22
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \geyao\geyao\使用代理模式预加载图片220330.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>使用代理模式加载图片</title>
</head>

<body>
  <script>
    var myImage = (function () {
      var imgNode = document.createElement("img");
      document.body.appendChild(imgNode);
      return {
        setSrc: function (src) {
          imgNode.src = src;
        }
      }
    })();
    // 代理模式
    var ProxyImage = (function () {
      var img = new Image();
      img.onload = function () {
        myImage.setSrc(this.src);
      };
      return {
        setSrc: function (src) {
          myImage.setSrc("http://img.lanrentuku.com/img/allimg/1212/5-121204193Q9-50.gif");
          img.src = src;
        }
      }
    })();
    // 调用方式
    ProxyImage.setSrc("https://img.alicdn.com/tps/i4/TB1b_neLXXXXXcoXFXXc8PZ9XXX-130-200.png");
  </script>
</body>

</html>